package com.example.opengldemo.vertex_magical;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.opengldemo.R;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.vertex_magical.shader.ArrayRenderer;
import com.example.opengldemo.vertex_magical.shader.BallRenderer;
import com.example.opengldemo.vertex_magical.shader.EagleRenderer;
import com.example.opengldemo.vertex_magical.shader.WaterRealShader;
import com.example.opengldemo.vertex_magical.shader.WaterRenderer;
import com.example.opengldemo.vertex_magical.shader.filter.FilterRenderer;

import static android.view.View.*;
import static com.example.opengldemo._11_.KeyThread.up;

public class FrameDemoActivity extends Activity {

    private SurfaceView mSurfaceView;
    private FrameProcessorProxy processorProxy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);
        mSurfaceView = (SurfaceView) findViewById(R.id.mSurfaceView);

        initView() ;

        processorProxy = new FrameProcessorProxy(getApplicationContext());

        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pm);
                if (bitmap != null) {
                    MatrixState.setInitStack();
                    ArrayRenderer arrayRenderer = new ArrayRenderer() ;

                   //arrayRenderer.addRenderer(new WaterRealShader(getResources() , "vertexmagical/shake_vertex.sh" , "vertexmagical/shake_frag.sh"));
                   //arrayRenderer.addRenderer(new EagleRenderer(getResources() , "vertexmagical/eagle_vertex.sh" , "vertexmagical/eagle_frag.sh"));
                   arrayRenderer.addRenderer(new FilterRenderer(getResources() , "vertexmagical/filter/smooth_filter_vertex.sh" , "vertexmagical/filter/smooth_filter_frag.sh"));
                    processorProxy.setRenderer(arrayRenderer);
                    //processorProxy.setRenderer(new WaterRealShader(getResources() , "vertexmagical/shake_vertex.sh" , "vertexmagical/shake_frag.sh"));
                }

                MatrixState.cameraLocation[0] = 0 ;
                MatrixState.cameraLocation[1] = 0 ;
                MatrixState.cameraLocation[2] = 4 ;
                MatrixState.projectLocation[4] = 2 ;
                MatrixState.projectLocation[5] = 4 ;
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                processorProxy.setProperty(FrameProcessorProxy.KEY_PREVIEW_WIDTH, width);
                processorProxy.setProperty(FrameProcessorProxy.KEY_PREVIEW_HEIGHT, height);
                processorProxy.open(0);
                processorProxy.setPreviewSurface(holder.getSurface());
                processorProxy.startPreview();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                processorProxy.stopPreview();
                processorProxy.close();
            }
        });
    }
    private void log(){
        Log.e("MatrixState","MatrixState \n"+"project-near > "+MatrixState.projectLocation[4] +"\n"+
                "project-far > "+MatrixState.projectLocation[5] +"\n"+
                "camera-X > "+MatrixState.translate[0] +"\n"+
                "camera-Y > "+MatrixState.translate[1] +"\n"+
                "camera-Z > "+MatrixState.translate[2] +"\n") ;
    }

    private void initView() {
        findViewById(R.id.near_add).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                float near  = MatrixState.projectLocation[4];
                near = near+0.2f  ;
                MatrixState.projectLocation[4] = near ;
                log() ;
            }
        });
        findViewById(R.id.near_del).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                float near  = MatrixState.projectLocation[4];
                near = near-0.2f  ;
                MatrixState.projectLocation[4] = near ;
                log() ;
            }
        });

        findViewById(R.id.far_add).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                float near  = MatrixState.projectLocation[5];
                near = near+0.2f  ;
                MatrixState.projectLocation[5] = near ;
                log() ;
            }
        });
        findViewById(R.id.far_del).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                float near  = MatrixState.projectLocation[5];
                near = near-0.2f   ;
                MatrixState.projectLocation[5] = near ;
                log() ;
            }
        });

        findViewById(R.id.C_X_add).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float near  = MatrixState.rotate[0];
                near = near+0.5f  ;
                MatrixState.rotate[0] = near ;
                log() ;
                return true;
            }
        });
        findViewById(R.id.C_X_del).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float near  = MatrixState.rotate[0];
                near = near-0.5f  ;
                MatrixState.rotate[0] = near ;
                log() ;
                return true;
            }
        });

        findViewById(R.id.C_Y_add).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float near  = MatrixState.rotate[1];
                near = near+0.5f  ;
                MatrixState.rotate[1] = near ;
                log() ;
                return true;
            }
        });
        findViewById(R.id.C_Y_del).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float near  = MatrixState.rotate[1];
                near = near-0.5f  ;
                MatrixState.rotate[1] = near ;
                log() ;
                return true;
            }
        });

        findViewById(R.id.C_Z_add).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float near  = MatrixState.translate[2];
                near = near+0.5f  ;
                MatrixState.translate[2] = near ;
                log() ;
                return true;
            }
        });
        findViewById(R.id.C_Z_del).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float near  = MatrixState.translate[2];
                near = near-0.5f ;
                MatrixState.translate[2] = near ;
                log() ;
                return true;
            }
        });


        findViewById(R.id.filter_open_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int near  = FilterRenderer.isOpen;
                if(near != 0){
                    near = 0 ;
                    v.setBackgroundColor(Color.GREEN);
                }else{
                    near++ ;
                    v.setBackgroundColor(Color.RED);
                }
                FilterRenderer.isOpen = near ;
            }
        });
        findViewById(R.id.filter_open_close_3m).setOnClickListener(v -> {

            View viewById = findViewById(R.id.filter_renderer_3x3);
            if(viewById.getVisibility() == GONE){
                viewById.setVisibility(VISIBLE);
            }else{
                viewById.setVisibility(GONE);
            }
        });

        findViewById(R.id.filter_luminance_add).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                FilterRenderer.luminance += 0.01 ;
                return true;
            }
        });

        findViewById(R.id.filter_luminance_del).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                FilterRenderer.luminance -= 0.01 ;
                return true;
            }
        });

        // ----------------------
        findViewById(R.id.filter_magTol_add).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                FilterRenderer.uf_MagTol += 0.01 ;
                return true;
            }
        });

        findViewById(R.id.filter_magTol_del).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                FilterRenderer.uf_MagTol -= 0.01 ;
                return true;
            }
        });
        // -----------------------
        findViewById(R.id.filter_Quantize_add).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                FilterRenderer.uf_Quantize += 1 ;
                return true;
            }
        });

        findViewById(R.id.filter_Quantize_del).setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                FilterRenderer.uf_Quantize -= 1 ;
                return true;
            }
        });

        initView3X3(FilterRenderer.matrix3x3) ;
    }

    private void initView3X3(final float[][] intMatrix) {
        up(intMatrix) ;
        center(intMatrix) ;
        bottom(intMatrix)  ;
    }

    private void up(final float[][] intMatrix){
        //------------------------ left up
        findViewById(R.id.left_up_add).setOnClickListener((view) ->{
            intMatrix[0][0] ++ ;
            ((TextView)findViewById(R.id.left_up_del)).setText(intMatrix[0][0]+"");
        });
        findViewById(R.id.left_up_del).setOnClickListener((view) ->{
            intMatrix[0][0] -- ;
            ((TextView)findViewById(R.id.left_up_del)).setText(intMatrix[0][0]+"");
        });
        //------------------------ center up
        findViewById(R.id.center_up_add).setOnClickListener((view) ->{
            intMatrix[0][1] ++ ;
            ((TextView)findViewById(R.id.center_up_del)).setText(intMatrix[0][1]+"");
        });
        findViewById(R.id.center_up_del).setOnClickListener((view) ->{
            intMatrix[0][1] -- ;
            ((TextView)findViewById(R.id.center_up_del)).setText(intMatrix[0][1]+"");
        });

        //------------------------ right up
        findViewById(R.id.right_up_add).setOnClickListener((view) ->{
            intMatrix[0][2] ++ ;
            ((TextView)findViewById(R.id.right_up_del)).setText(intMatrix[0][2]+"");
        });
        findViewById(R.id.right_up_del).setOnClickListener((view) ->{
            intMatrix[0][2] -- ;
            ((TextView)findViewById(R.id.right_up_del)).setText(intMatrix[0][2]+"");
        });
    }

    private void center(final float[][] intMatrix){
        //------------------------ left up
        findViewById(R.id.left_center_add).setOnClickListener((view) ->{
            intMatrix[1][0]++ ;
            ((TextView)findViewById(R.id.left_center_del)).setText(intMatrix[1][0]+"");
        });
        findViewById(R.id.left_center_del).setOnClickListener((view) ->{
            intMatrix[1][0] -- ;
            ((TextView)findViewById(R.id.left_center_del)).setText(intMatrix[1][0]+"");
        });
        //------------------------ center up
        findViewById(R.id.center_center_add).setOnClickListener((view) ->{
            intMatrix[1][1]++ ;
            ((TextView)findViewById(R.id.center_center_del)).setText(intMatrix[1][1]+"");
        });
        findViewById(R.id.center_center_del).setOnClickListener((view) ->{
            intMatrix[1][1] -- ;
            ((TextView)findViewById(R.id.center_center_del)).setText(intMatrix[1][1]+"");
        });

        //------------------------ right up
        findViewById(R.id.right_center_add).setOnClickListener((view) ->{
            intMatrix[1][2] ++ ;
            ((TextView)findViewById(R.id.right_center_del)).setText(intMatrix[1][2]+"");
        });
        findViewById(R.id.right_center_del).setOnClickListener((view) ->{
            intMatrix[1][2] -- ;
            ((TextView)findViewById(R.id.right_center_del)).setText(intMatrix[1][2]+"");
        });
    }

    private void bottom(final float[][] intMatrix){
        //------------------------ left up
        findViewById(R.id.left_bottom_add).setOnClickListener((view) ->{
            intMatrix[2][0]++ ;
            ((TextView)findViewById(R.id.left_bottom_del)).setText(intMatrix[2][0]+"");
        });
        findViewById(R.id.left_bottom_del).setOnClickListener((view) ->{
            intMatrix[2][0] -- ;
            ((TextView)findViewById(R.id.left_bottom_del)).setText(intMatrix[2][0]+"");
        });
        //------------------------ center up
        findViewById(R.id.center_bottom_add).setOnClickListener((view) ->{
            intMatrix[2][1]++ ;
            ((TextView)findViewById(R.id.center_bottom_del)).setText(intMatrix[2][1]+"");
        });
        findViewById(R.id.center_bottom_del).setOnClickListener((view) ->{
            intMatrix[2][1] -- ;
            ((TextView)findViewById(R.id.center_bottom_del)).setText(intMatrix[2][1]+"");
        });

        //------------------------ right up
        findViewById(R.id.right_bottom_add).setOnClickListener((view) ->{
            intMatrix[2][2] ++ ;
            ((TextView)findViewById(R.id.right_bottom_del)).setText(intMatrix[2][2]+"");
        });
        findViewById(R.id.right_bottom_del).setOnClickListener((view) ->{
            intMatrix[2][2] -- ;
            ((TextView)findViewById(R.id.right_bottom_del)).setText(intMatrix[2][2]+"");
        });
    }
}
